/**
 * MinIO 配置和工具函数
 */

// 获取MinIO服务器基础URL
export const getMinioBaseUrl = () => {
  return import.meta.env.VITE_MINIO_BASE_URL || 'http://localhost:9000'
}

// 构建MinIO对象URL
export const buildMinioUrl = (bucketName, objectPath) => {
  const baseUrl = getMinioBaseUrl()
  // 确保路径格式正确
  const cleanPath = objectPath.startsWith('/') ? objectPath.slice(1) : objectPath
  return `${baseUrl}/${bucketName}/${cleanPath}`
}

// 构建图片URL（专门用于图片资源）
export const buildImageUrl = (bucketName, imagePath) => {
  return buildMinioUrl(bucketName, imagePath)
}

// 预设的存储桶配置
export const MINIO_BUCKETS = {
  IMAGES: 'images',        // 图片存储桶
  AVATARS: 'avatars',      // 头像存储桶
  PETS: 'pets',            // 宠物图片存储桶
  ITEMS: 'items',          // 道具图片存储桶
  FASHION: 'fashion'       // 时装图片存储桶
}

// 根据物品类型获取对应的存储桶
export const getBucketByItemType = (itemType) => {
  switch (itemType) {
    case 1:
    case 'pet':
      return MINIO_BUCKETS.PETS
    case 2:
    case 'item':
      return MINIO_BUCKETS.ITEMS
    case 3:
    case 'fashion':
      return MINIO_BUCKETS.FASHION
    default:
      return MINIO_BUCKETS.IMAGES
  }
}

// 构建物品图片URL
export const buildItemImageUrl = (itemId, itemType) => {
  const bucket = getBucketByItemType(itemType)
  const imagePath = `${itemId}.png`
  return buildMinioUrl(bucket, imagePath)
}

export default {
  getMinioBaseUrl,
  buildMinioUrl,
  buildImageUrl,
  buildItemImageUrl,
  getBucketByItemType,
  MINIO_BUCKETS
}